package com.base.jdk8.stream;

import com.base.jdk8.dto.Salary;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestStream {
    //从员工集合中筛选出salary大于8000的员工，并放置到新的集合里。
    public static void filter() {
        List<Salary> salaries = Arrays.asList(new Salary(BigDecimal.valueOf(7000)),
                                              new Salary(BigDecimal.valueOf(6000)),
                                              new Salary(BigDecimal.valueOf(30000)),
                                              new Salary(BigDecimal.valueOf(50000)),
                                              new Salary(BigDecimal.valueOf(100000)));
        List<Salary> result =
                salaries.stream().filter(salary -> salary.getMoney().doubleValue() > 8000).collect(Collectors.toList());
        System.out.println("result = " + result);
    }

    //统计员工的最高薪资、平均薪资、薪资之和
    public static void avg() {
        List<Salary> salaries = Arrays.asList(new Salary(BigDecimal.valueOf(7000)),
                                              new Salary(BigDecimal.valueOf(6000)),
                                              new Salary(BigDecimal.valueOf(30000)),
                                              new Salary(BigDecimal.valueOf(50000)),
                                              new Salary(BigDecimal.valueOf(100000)));
        salaries.parallelStream();
    }

    public static void create() {
        Stream.of(1, 2, 3, 4, 5).limit(3).forEach(System.out::println);
        Stream.generate(Math::random).forEach(System.out::println);
    }

    public static void filter2() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        list.stream().filter(x -> x > 3).forEach(System.out::println);
        Optional<Integer> first = list.stream().filter(x -> x > 3).findFirst();
        System.out.println("first = " + first);
        Optional<Integer> any = list.stream().parallel().findAny();
        System.out.println("any = " + any);
        boolean anyMatch = list.stream().anyMatch(x -> x > 6);
        System.out.println("anyMatch = " + anyMatch);
    }

    public static void max() {
        List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
        String s = list.stream().max(Comparator.comparing(String::length)).get();
        System.out.println("s = " + s);
    }

    public static void main(String[] args) {
        //filter();
        //create();
        //filter2();
        max();
    }

}
